package cn.sfrog.project.biz.admin.controller;

import cn.sfrog.common.annotation.Log;
import cn.sfrog.common.annotation.RepeatSubmit;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.AjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.enums.BusinessType;
import cn.sfrog.common.enums.StatusEnum;
import cn.sfrog.common.utils.poi.ExcelUtil;
import cn.sfrog.project.biz.admin.domain.vo.BizDepartmentFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizDepartmentQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizDepartmentVO;
import cn.sfrog.project.biz.admin.service.IBizDepartmentService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "后台-科室相关")
@RestController
@RequestMapping("/biz/department")
public class BizDepartmentController extends BaseController {
    @Resource
    private IBizDepartmentService bizDepartmentService;

    /**
     * 查询科室列表
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('biz:department:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizDepartmentQueryVO queryVO) {
        startPage();
        List<BizDepartmentVO> list = bizDepartmentService.selectBizDepartmentList(queryVO);
        return getDataTable(list);
    }

    /**
     * 查询医院下所有的科室列表
     * @param hospitalId
     * @return
     */
    @ApiOperation("查询医院下所有的科室列表")
    @GetMapping("/getAllDepartmentList/{hospitalId}")
    public AjaxResult getAllDepartmentListByHospital(@PathVariable("hospitalId") Long hospitalId){
        BizDepartmentQueryVO queryVO = new BizDepartmentQueryVO();
        queryVO.setHospitalId(hospitalId);
        queryVO.setStatus(StatusEnum.active.toString());
        List<BizDepartmentVO> list = bizDepartmentService.selectBizDepartmentList(queryVO);
        return AjaxResult.success(list);
    }

    /**
     * 查询医院下所有的科室列表-分页
     * @param
     * @return
     */
    @ApiOperation("查询医院下所有的科室列表-分页")
    @GetMapping("/getAllDepartmentPage")
    public TableDataInfo getAllDepartmentPageByHospital(BizDepartmentQueryVO queryVO){
        startPage();
        List<BizDepartmentVO> list = bizDepartmentService.selectBizDepartmentList(queryVO);
        return getDataTable(list);
    }


    /**
     * 导出科室列表
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('biz:department:export')")
    @Log(title = "科室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizDepartmentQueryVO query) {
        List<BizDepartmentVO> list = bizDepartmentService.selectBizDepartmentList(query);
        ExcelUtil<BizDepartmentVO> util = new ExcelUtil<BizDepartmentVO>(BizDepartmentVO.class);
        util.exportExcel(response, list, "科室数据");
    }

    /**
     * 获取科室详细信息
     */
    @ApiOperation("获取详情")
    @PreAuthorize("@ss.hasPermi('biz:department:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizDepartmentService.selectBizDepartmentById(id));
    }

    /**
     * 新增科室
     */
    @ApiOperation("新增")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:department:add')")
    @Log(title = "科室", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizDepartmentFormVO form) {
        return toAjax(bizDepartmentService.insertBizDepartment(form));
    }

    /**
     * 修改科室
     */
    @ApiOperation("修改")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:department:edit')")
    @Log(title = "科室", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizDepartmentFormVO form) {
        return toAjax(bizDepartmentService.updateBizDepartment(form));
    }

    /**
     * 删除科室
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('biz:department:remove')")
    @Log(title = "科室", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizDepartmentService.deleteBizDepartmentByIds(ids));
    }

    /**
     * 更改状态科室
     */
    @ApiOperation("更改状态")
    @PreAuthorize("@ss.hasAnyPermi('biz:department:changeStatus,biz:department:changeStatusBatch')")
    @Log(title = "科室", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Valid BizStatusChangeVO vo) {
        return AjaxResult.success(bizDepartmentService.changeStatus(vo));
    }
}
